package org.jeecg.modules.xindian.reconciliation.service.impl;

import cn.hutool.core.lang.Assert;
import org.jeecg.modules.xindian.reconciliation.entity.*;
import org.jeecg.modules.xindian.reconciliation.enums.ChannelSettlementConfigType;
import org.jeecg.modules.xindian.reconciliation.mapper.ChannelSettlementConfigMapper;
import org.jeecg.modules.xindian.reconciliation.service.IChannelSettlementConfigService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static org.jeecg.modules.xindian.reconciliation.enums.DeleteFlagEnum.N;
import static org.jeecg.modules.xindian.reconciliation.service.impl.ChannelSettlementInfoServiceImpl.ALL;
import static org.jeecg.modules.xindian.reconciliation.service.impl.InsuranceSettlementConfigServiceImpl.checkBetween;

/**
 * @Description: 渠道结算配置
 * @Author: jeecg-boot
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Service
public class ChannelSettlementConfigServiceImpl extends ServiceImpl<ChannelSettlementConfigMapper, ChannelSettlementConfig> implements IChannelSettlementConfigService {
    @Override
    public boolean save(ChannelSettlementConfig entity) {
        validate(entity);
        entity.buildConfig();
        return super.save(entity);
    }


    private void validate(ChannelSettlementConfig config) {
        Assert.notBlank(config.getChannel(), "渠道名称不能为空");
        Assert.notEmpty(config.getConfigs(), "渠道配置信息不能为空");
        Assert.notNull(config.getInvoiceRate(), "发票点数不能为空");
        AtomicReference<Boolean> haveOTHERConfig = new AtomicReference<>(false);
        //对保险公司及
        Map<String,List<ChannelSettlementConfig.Config>> insuranceCompanyAreaGroup =  config.getConfigs().stream()
                .collect(Collectors.groupingBy(ChannelSettlementConfig.Config::getInsuranceCompany));
        config.getConfigs().forEach(ct -> {
            String insuranceCompany = ct.getInsuranceCompany();
            Assert.notBlank(insuranceCompany, "保险公司不能为空");
            Assert.notEmpty(ct.getPlatform(), "{}下的平台不能为空", insuranceCompany);
            Assert.notNull(ct.getType(), "{}下的结算类型不能为空", insuranceCompany);
            Assert.notNull(ct.getFixRate(), "{}下的结算比例不能为空", insuranceCompany);
            checkBetween(ct.getFixRate(), BigDecimal.ZERO, BigDecimal.ONE, "结算比例");
            if (ChannelSettlementConfigType.OTHER.equals(ct.getType())) {
                haveOTHERConfig.set(true);
                Assert.notNull(ct.getOtherPolicyRate(), "{}的结算类型为结算其他保单,结算其他保单比例不能为空", insuranceCompany);
                checkBetween(ct.getOtherPolicyRate(), BigDecimal.ZERO, BigDecimal.ONE, "其他保单比例");
            }
        });
        if(haveOTHERConfig.get()){
            Assert.isTrue(config.getConfigs().size() ==1,"配置了其他结算其他保单只能配置一个平台的结算比例");
        }

    }

    @Override
    public boolean updateById(ChannelSettlementConfig entity) {
        validate(entity);
        entity.buildConfig();
        return super.updateById(entity);
    }


    @Override
    public List<ChannelSettlementConfig> queryEnableConfig() {
        return lambdaQuery().eq(ChannelSettlementConfig::getDeleteFlag,N).list();
    }

    @Override
    public ChannelSettlementConfig queryByChannelName(String channelName) {
        return lambdaQuery().eq(ChannelSettlementConfig::getChannel,channelName).eq(ChannelSettlementConfig::getDeleteFlag,N).one();
    }


}
